import { ref } from 'vue'
import { defineStore } from 'pinia'
import { getDataByIP, getCode, getTodayWeather, getRecentWeather } from '@/api/index.js'

export const useWeatherStore = defineStore('weather', () => {
  const cityName = ref('')
  const localStorageCityName = ref('')

  const weatherData = ref('')
  const weatherDisplay = ref('')
  const weatherRencent = ref('')


  const getipWeather = async () => {
    const { adcode } = await getDataByIP()

    const { lives } = await getTodayWeather(adcode)

    weatherData.value = lives[0]
  }
  const fetchWeatherData = async (cityCode, isRecent = false) => {
    const { lives, forecasts } = isRecent ? await getRecentWeather(cityCode) : await getTodayWeather(cityCode)
    return isRecent ? forecasts[0].casts : lives[0]
  }

  const getCurrentWeather = async () => {
    const { geocodes } = await getCode(cityName.value)
    const code = geocodes[0].adcode
    weatherDisplay.value = await fetchWeatherData(code)
  }

  const getIPReportWeather = async () => {
    const { adcode } = await getDataByIP()
    weatherRencent.value = await fetchWeatherData(adcode, true)
  }

  const getSearchReportWeather = async () => {
    const { geocodes } = await getCode(cityName.value)
    const code = geocodes[0].adcode
    weatherRencent.value = await fetchWeatherData(code, true)
  }

  const getLocalStorageReportWeather = async () => {
    const { geocodes } = await getCode(localStorageCityName.value)
    const code = geocodes[0].adcode
    weatherRencent.value = await fetchWeatherData(code, true)
  }

  const formatWeatherDates = (casts) => {
    const weekMap = {
      1: '周一',
      2: '周二',
      3: '周三',
      4: '周四',
      5: '周五',
      6: '周六',
      0: '周日',
    }
    return casts.map((cast, index) => {
      cast.date = cast.date.replace(/^\d{4}-/, '')
      if (index === 0) {
        cast.week = '今天'
      } else if (index === 1) {
        cast.week = '明天'
      } else {
        cast.week = weekMap[cast.week]
      }
      return cast
    })
  }

  const formatDate = () => {
    if (weatherRencent.value) {
      weatherRencent.value = formatWeatherDates(weatherRencent.value)
    }
  }

  const getCityListFromStorage = () => {
    return JSON.parse(localStorage.getItem('cityList')) || []
  }

  const setCityListToStorage = (cityList) => {
    localStorage.setItem('cityList', JSON.stringify(cityList))
  }

  const getLocalStorageCurrentWeather = async () => {
    const { geocodes } = await getCode(localStorageCityName.value)
    const code = geocodes[0].adcode
    const { lives } = await getTodayWeather(code)
    weatherDisplay.value = lives[0]
  }

  const addCity = async () => {
    await getCurrentWeather()
    const city = {
      id: Date.now(),
      name: cityName.value,
      temperature: weatherDisplay.value.temperature,
    }
    const cityList = getCityListFromStorage()
    const cityExists = cityList.some((existingCity) => existingCity.name === city.name)
    if (cityExists) {
      alert('该城市已存在！')
      return
    }
    cityList.push(city)
    setCityListToStorage(cityList)
  }
  const getCityList = getCityListFromStorage
  const removeCity = (id) => {
    const cityList = getCityListFromStorage()
    const updatedList = cityList.filter((city) => city.id !== id)
    setCityListToStorage(updatedList)
  }

  const checkLocalStorageCityName = (name) => {
    localStorageCityName.value = name
  }

  return {
    cityName,
    weatherData,
    weatherDisplay,
    weatherRencent,
    localStorageCityName,
    getipWeather,
    getCurrentWeather,
    getIPReportWeather,
    getSearchReportWeather,
    getLocalStorageReportWeather,
    formatDate,
    addCity,
    getCityList,
    removeCity,
    checkLocalStorageCityName,
    getLocalStorageCurrentWeather,
  }
})
